package linter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import linter.POJO.DO.Model;
import linter.POJO.DTO.ModelDTO;
import linter.mapper.ModelMapper;
import linter.service.ModelService;
import linter.utils.PropertyUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static linter.custom.CustomResultType.VIEWS_ERROR;

/**
* @author 北煜
* @description 针对表【model(模型基础信息表)】的数据库操作Service实现
* @createDate 2024-04-16 15:10:09
*/
@Service
public class ModelServiceImpl extends ServiceImpl<ModelMapper, Model>
    implements ModelService{
    @Resource
    ModelMapper modelMapper;

    @Override
    public PageInfo<Model> getAllModel() {
        QueryWrapper<Model> queryWrapper = new QueryWrapper<>();
        List<Model> list = modelMapper.selectList(queryWrapper.eq("is_delete",0));
        PageHelper.startPage(1,10);
        PageInfo<Model> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Object selModel(String modelName, Integer modelNub, String introduce) {
        QueryWrapper<Model> queryWrapper = new QueryWrapper<>();
        // 第一次尝试按 modelName 查询
        if (modelName != null) {
            List<Model> firstResult = modelMapper.selectList(queryWrapper
                    .like("model_name", "%" + modelName + "%"));
            List<ModelDTO> res = PropertyUtils.listToList(firstResult, ModelDTO.class);
            if (res.isEmpty()) {
                QueryWrapper<Model> qw = new QueryWrapper<>();
                List<Model> secondResult = modelMapper.selectList(qw
                        .like("model_english_name", "%" + modelName + "%"));
                return PropertyUtils.listToList(secondResult, ModelDTO.class);
            } else if (modelNub == null && introduce == null) {
                return res;
            }
        } else if (modelNub != null) {
            List<Model> Result = modelMapper.selectList(queryWrapper.eq("model_nub", modelNub));
            List<ModelDTO> resTwo = PropertyUtils.listToList(Result, ModelDTO.class);
            return resTwo;
        } else if (introduce != null) {
            List<Model> Result = modelMapper.selectList(queryWrapper
                    .like("model_introduce", "%" + introduce + "%"));
            return PropertyUtils.listToList(Result, ModelDTO.class);
        }
        return VIEWS_ERROR;
    }
}




